/**
 * 
 */
package com.csair.youth.web.action;

import org.apache.commons.lang.StringUtils;

import com.csair.youth.database.SysConfig;
import com.csair.youth.service.impl.SysConfigServiceImpl;

/**
 * @author mickey
 *
 */
public class SysConfigEditAction extends _BaseAction {
	private SysConfig item;
	private SysConfigServiceImpl service;
	
	public SysConfig getItem() {
		return item;
	}

	public void setItem(SysConfig item) {
		this.item = item;
	}

	public SysConfigServiceImpl getService() {
		return service;
	}

	public void setService(SysConfigServiceImpl service) {
		this.service = service;
	}

	public String execute(){
		if(item != null && !StringUtils.isEmpty(item.getId())){
			//从数据库获取...
			item = service.getSysConfig(item.getId());
		}
		else item = new SysConfig();
		return SUCCESS;
	}
	
	public String save(){
		if(item == null) {
			this.setMessage("变量对象为空.");
			return INPUT;
		}
		if(!validInput(item)) return INPUT;
		service.saveSysConfig(item);
		this.setMessage("已成功保存变量.");
		return SUCCESS;
	}
	
	private boolean validInput(SysConfig item){
		if(item == null) return false;
		boolean allValid = true;
		StringBuffer msgBf = new StringBuffer();
		String name = item.getName();
		if(StringUtils.isEmpty(name) || StringUtils.length(name)> 25){
			msgBf.append("变量名不能为空且必须在25个字符内<br/>");
			allValid = false;
		}
		String key = item.getConfigKey();
		if(StringUtils.isEmpty(key) || key.getBytes().length> 50){
			msgBf.append("变量key不能为空且必须在50个字节内<br/>");
			allValid = false;
		}
		String value = item.getConfigValue();
		if(StringUtils.isEmpty(value) || StringUtils.length(value)> 25){
			msgBf.append("变量值不能为空且必须在25个字符内<br/>");
			allValid = false;
		}
		String desc = item.getRemark();
		if(!StringUtils.isEmpty(desc) && StringUtils.length(desc)> 120){
			msgBf.append("变量备注必须在120个字符内<br/>");
			allValid = false;
		}
		this.setMessage(msgBf.toString());
		return allValid;
	}
}
